import requests
import json
import hashlib
from time import time
from app.libs.tools import signstr
from app.taobaoServer.config import *

class OrderApi():

    def __init__(self, accessToken):

        self.host = "http://gw.api.agiso.com/alds/Trade/TradeInfo"
        self.logistics_host = "http://gw.api.agiso.com/alds/Trade/LogisticsDummySend"
        self.accessToken = accessToken
        self.appsecret = '3gnffc36f3zy7gv7yuzrg74ph7mbxxp6'

    def get_order(self,tid):
        curtime  = int(time())
        headers = {
                'Authorization': 'Bearer '+self.accessToken,
                'ApiVersion': '1'
            }
        pre_body = {
                'tid': tid,
                'timestamp': str(curtime)
            }
        body = {
                'tid': tid,
                'timestamp': str(curtime),
                'sign': signstr(pre_body, self.appsecret)
        }
        r = requests.post(host, headers=headers, data=body)
        order_info = json.loads(r.text)

        return order_info

    def Logistics(self, tid):
        curtime  = int(time())
        headers = {
                'Authorization': 'Bearer '+self.accessToken,
                'ApiVersion': '1'
            }
        pre_body = {
                'tids': tid,
                'timestamp': str(curtime)
            }
        body = {
                'tids': tid,
                'timestamp': str(curtime),
                'sign': signstr(pre_body, self.appsecret)
        }
        r = requests.post(self.logistics_host, headers=headers, data=body)
        logistics_info = json.loads(r.text)
        print(logistics_info)
        return logistics_info

if __name__ == '__main__':
    r = OrderApi('TbAldszyw2zfa35ttemzxhbh2zkx27x6mabxssreswt2vkxgxt')

    print(r.get_order('2217144182649658454'))

# 263670919707554895,263671143605554895,263670759829554895,263497574354554895

